﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using Sgml;

namespace CommonLibrary.HtmlXpath
{
    public static class HtmlXpathClass
    {
        public static List<string> HtmlXpathSelectList(string in_html, string xpath)
        {
            List<string> list = new List<string>();

            using (SgmlReader sgmlReader = new SgmlReader())
            {
                sgmlReader.DocType = "HTML";
                sgmlReader.InputStream = new StringReader(in_html);

                using (StringWriter strWriter = new StringWriter())
                {
                    using (XmlTextWriter xmlWriter = new XmlTextWriter(strWriter))
                    {
                        xmlWriter.Formatting = Formatting.Indented;
                        while (sgmlReader.Read())
                        {
                            if (sgmlReader.NodeType != XmlNodeType.Whitespace)
                            {
                                xmlWriter.WriteNode(sgmlReader, true);
                            }
                        }
                        string formatHTML = strWriter.ToString();

                        //XPathDocument是只读的
                        XPathDocument doc = new XPathDocument(new StringReader(formatHTML));
                        XPathNavigator nav = doc.CreateNavigator();
                        XPathNodeIterator nodes = nav.Select(xpath);
                        while (nodes.MoveNext())
                        {
                            list.Add(nodes.Current.OuterXml);
                        }
                        return list;
                    }
                }
            }
        }

        public static string HtmlXpathSelect(string in_html, string xpath)
        {
            List<string> list = HtmlXpathSelectList(in_html, xpath);

            string result = "";
            foreach (string s in list)
            {
                result += Environment.NewLine + s;
            }
            if (result.Length > 0)
            {
                result = result.Substring(2);
            }
            return result;
        }

        public static string HtmlXpathDelete(string in_html, string xpath)
        {
            string result = "";

            using (SgmlReader sgmlReader = new SgmlReader())
            {
                sgmlReader.DocType = "HTML";
                sgmlReader.InputStream = new StringReader(in_html);

                using (StringWriter strWriter = new StringWriter())
                {
                    using (XmlTextWriter xmlWriter = new XmlTextWriter(strWriter))
                    {
                        xmlWriter.Formatting = Formatting.Indented;
                        while (sgmlReader.Read())
                        {
                            if (sgmlReader.NodeType != XmlNodeType.Whitespace)
                            {
                                xmlWriter.WriteNode(sgmlReader, true);
                            }
                        }
                        string formatHTML = strWriter.ToString();

                        //XPathDocument是只读的，XmlDocument是可写的
                        //XPathDocument doc = new XPathDocument(new StringReader(formatHTML));
                        XmlDocument doc = new XmlDocument();
                        doc.Load(new StringReader(formatHTML));

                        XPathNavigator nav = doc.CreateNavigator();
                        XPathNodeIterator nodes = nav.Select(xpath);
                        while (nodes.MoveNext())
                        {
                            nodes.Current.DeleteSelf();
                        }

                        result = nav.OuterXml;
                        return result;
                    }
                }
            }
        }
    }
}
